<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
use Admin\Controller\PublicController;
class StudentadminController extends AdminbaseController {
	
	protected $m;
	protected $ls;
	protected $srm;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('LessonStudentRelation');
		$this->ls = D('YgclLesson');
		$this->srm = D('LessonRecord');
	}
	
    public function index(){
    	$this->display();
    }
    
    public function edit(){
    	
    	$id = I('get.id');
    	if($id){
    		$amap['id'] = $id;
    		$this->info = $this->m->where($amap)->find();
    	}
    	$this->xueqi = R('Api/Term/get_term_infos_for_limit');
    	
    	$did = R('Apiadmin/getxiaoqurole');
    	if($did){
    		$map['id'] = $did;
    	}
    	$map['status'] = 1;
		$this->xiaoqu = D('YgclDistrict')->where($map)->order('listorder desc')->select();
		$cUser = $this->curUser;
		$this->cUser = $cUser;
    	$this->display();
    }
    
    public function check_number($tid,$lid){
    	$terminfo = R('Api/Term/get_term_by_id',array('id'=>$tid));
    	$xuenian = $terminfo['studyyear'];
    	$data = R('Api/Lessonapi/getnumber_by_xuenian',array('xuenian'=>$xuenian,'lid'=>$lid));
    	$status = array('status'=>1,'info'=>'');
    	if(!$data['bool']){
    		$xnid = $data['xnid'];
    		$info = M('YgclTermTime')->where(array('id'=>$xnid))->find();
    		$html = '此班级('.$info['start_time'].'到'.$info['end_time'].')人数已报满,请选择其他班级或有特殊情况请联系校方递交申请';
    		$status = array('status'=>0,'info'=>$html);
    	}
    	return $status;
    }
    
    public function edit_post(){
    	if(IS_POST){
    		if($_POST['id']){
    			$lsid = $_POST['id'];
    			$lsinfo = $this->m->where(array('id'=>$lsid))->find();
    			if(I('tid') == $lsinfo['tid'] && I('lid') == $lsinfo['lid']){
    				$data = array(
    						'id' => $lsid,
    						'student' => I('student'),
    						'tel' => I('tel'),
    						'jzxm' => I('jzxm'),
    						'remark' => I('remark'),
    						'update_time' => Date('Y-m-d H:i:s')
    				);
    				if($this->m->save($data)){

    					R('Api/Student/uid_relation_by_tel',array($lsid,$_POST['tel']));
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}else{
    				$status = $this->check_number(I('tid'), I('lid'));
    				if(!$status['status']){
    					$this->error("超过班级人数");
    					exit();
    				}
    				$terminfo = R('Api/Term/get_term_by_id',array('id'=>I('tid')));
    				$data = array(
    						'id' => $lsid,
    						'student' => I('student'),
    						'tel' => I('tel'),
    						'jzxm' => I('jzxm'),
    						'tid' => I('tid'),
    						'xuenian' => $terminfo['studyyear'],
    						'did' => I('did'),
    						'cid' => I('cid'),
    						'lid' => I('lid'),
    						'end_time' => $terminfo['end_time'],
    						'remark' => I('remark'),
    						'update_time' => Date('Y-m-d H:i:s')
    				);
    				if($this->m->save($data)){

    					//保存换班记录    					
						$uid = $this->curUser['uid'];
    					R('Api/Record/insertRecord',array('uid'=>$uid,'lsid'=>$lsid,'original_tid'=>$lsinfo['tid'],'next_tid'=>I('tid'),'original_lid'=>$lsinfo['lid'],'next_lid'=>I('lid'),'remark'=>I('remark')));
    					
    					//更换人数
    					R('Api/Lessonapi/changelsinfo',array('old_tid'=>$lsinfo['tid'],'tid'=>I('tid'),'old_lid'=>$lsinfo['lid'],'lid'=>I('lid')));
    					//关联用户表的uid
    					R('Api/Student/uid_relation_by_tel',array($lsid,$_POST['tel']));
    						
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$status = $this->check_number(I('tid'), I('lid'));
    			if(!$status['status']){
    				$this->error("超过班级人数");
    				exit();
    			}
    			$terminfo = R('Api/Term/get_term_by_id',array('id'=>I('tid')));
    			$data = array(
    					'student' => I('student'),
    					'tel' => I('tel'),
    					'jzxm' => I('jzxm'),
    					'tid' => I('tid'),
    					'xuenian' => $terminfo['studyyear'],
    					'did' => I('did'),
    					'cid' => I('cid'),
    					'lid' => I('lid'),
    					'end_time' => $terminfo['end_time'],
    					'remark' => I('remark'),
    					'create_time' => Date('Y-m-d H:i:s'),
    					'update_time' => Date('Y-m-d H:i:s')
    			);
    			$id = $this->m->add($data);
    			if($id){
    				
    				//添加学员记录
    				$uid = $this->curUser['uid'];
    				R('Api/Record/addRecord',array('uid'=>$uid,'lsid'=>$id,'remark'=>I('remark')));
    				
    				
    				R('Api/Lessonapi/lesson_number_inc',array('tid'=>I('tid'),'lid'=>I('lid')));
    				//关联用户表的uid
    				R('Api/Student/uid_relation_by_tel',array($id,$_POST['tel']));
    				$this->success('添加成功');
    			}else{
    				$this->error('添加失败');
    			}
    		}
    		
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		R('Api/Lessonapi/lesson_number_dec',array('tid'=>$info['tid'],'lid'=>$info['lid']));
    		$this->success('删除成功');
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changestatus(){
    	$data[$this->m->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($this->m->save($data)){
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
    
    //导出
    public function export(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('学生姓名','电话','家长姓名','学期','校区','课程','班级')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);
    	
    	$mapp = $_GET;
    	unset($mapp['_URL_']);
    	unset($mapp["__hash__"]);
    	$time1 = 0;
    	$time2 = date('Y-m-d H:i:s');
    	foreach($mapp as $k=>$v){
    		if($v){
    			if($k != 'start_time' && $k != 'end_time' && $k != 'p' && $k != 'searchtype' && $k != 'keywords'){
    				$map[$k] = $v;
    			}
    			else{
    				if($k == 'start_time') $time1 = $v;
    				if($k == 'end_time') $time2 = $v;
    			}
    			if($k != 'p'){
    				$path .= $k.'/'.$v.'/';
    			}
    				
    		}
    	}
    	if($_GET['searchtype'] && $_GET['keywords']){
    		$map[$_GET['searchtype']] = array('like','%'.$_GET['keywords'].'%');
    	}
    	$map['update_time'] = array('between',array($time1,$time2));
    	
    	$xiaoqu = R('Apiadmin/getxiaoqurole');
    	if($xiaoqu){
    		$map['did'] = array('in',$xiaoqu);
    	}
    	
    	
    	$info = D('LessonStudentRelation')->where($map)->relation(true)->order('id desc')->select();
    	foreach ($info as $key => $if){
    		$data[$key] = array(
    				'0' => $if['student'],
    				'1' => $if['tel'],
    				'2' => $if['jzxm'],
    				'3' => $if['term']['title'],
    				'4' => $if['xiaoqu']['title'],
    				'5' => $if['kecheng']['title'],
    				'6' => $if['banji']['lessonname']
    		);
    	}
    	 
    	$excel->export($data);
    }
    
    public function import(){
    	$this->display();
    }
    public function import_post(){
    	if(IS_POST){
    		header("Content-type: text/html; charset=utf-8");
    		$config = array(
    				'fields' => array('student','tel','jzxm','lessoncode','xiaoqu','remark')
    		);
    		$term  = A('Api/Term')->get_term_info();
    		$excel = new \Common\ORG\MiniwebExcel($config);
    		$datas = $excel->import('.'.$_POST['url']);
    		$m = D('LessonStudentRelation');
    		if($datas){
    			foreach ($datas as $dt){
    				$info = $this->getinfo($dt['lessoncode'],$dt['xiaoqu']);
    				if($info){
    					$d = array(
    							'student'=> $dt['student'],
    							'tel' => $dt['tel'],
    							'jzxm' => $dt['jzxm'],
    							'tid'=> $term['id'],
    							'did'=> $info['did'],
    							'cid'=> $info['cid'],
    							'lid'=> $info['lid'],
    							'remark' => $dt['remark'],
    							'create_time' => Date('Y-m-d H:i:s'),
    							'update_time' => Date('Y-m-d H:i:s')
    					);
    					$m->add($d);
    					R('Api/Lessonapi/lesson_number_inc',array('lid'=>$info['lid']));
    				}
    			}
    			$this->success('导入成功');
    		}else{
    			$this->error('导入失败');
    		}
    	}
    }
    
    public function getinfo($lessoncode,$xiaoqu){
    	$data = D('YgclLesson')->where(array('lessoncode'=>$lessoncode))->select();
    	$class = D('YgclClass');
    	foreach ($data as $key => $dt){
    		$did = $class->where(array('id'=>$dt['cid']))->getField('did');
    		if($did == $xiaoqu){
    			$info = array(
    				'did'=>$did,
    				'cid'=>$dt['cid'],
    				'lid'=>$dt['id']
    			);
    			break;
    		}
    	}
    	if($info){
    		return $info;
    	}else{
    		return false;
    	}
    }
}